<?php

namespace app\api\controller\shop;

use app\admin\model\Deviceorder;
use app\admin\model\Deviceorderdetail;
use app\admin\model\Usermno;
use app\api\service\DeviceService;
use app\api\service\VbillService;
use app\common\controller\Api;

/**
 * 首页接口
 */
class Order extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];
    protected $relationSearch = true;


    public function getList()
    {

        $user = $this->auth->getUser();
        $device_id = $this->request->param('device_id');
        $status = $this->request->param('status');
        $keyword = $this->request->param('keyword');
        $start = $this->request->param('start');
        $end = $this->request->param('end');
        $page = $this->request->param('page');

        $limit = 10;
        $offset = ($page - 1) * $limit;
        $where = array();
        if($device_id != ""){
            $where['Deviceorder.device_id'] = $device_id;
        }
        if($keyword != ""){
            $where['order_no'] = ['like', '%' . trim($keyword) . '%'];
        }
        if($start != ""){
            $start = $start." 00:00";
            $end =$end." 23:59";
            $where['Deviceorder.create_time'] = array("between",array($start,$end));
        }
        if($status != ""){
            $where['Deviceorder.status'] = $status;
        }

        $where['Deviceorder.agent_id'] = $user['id'];

        $deviceOrderModel  = new Deviceorder();
        $deviceOrderDetailModel = new Deviceorderdetail();
        $list = $deviceOrderModel->with(['user','device'])->where('Deviceorder.status',"<>",'paying')->where($where)->limit($offset,$limit)->order('create_time','desc')->select();
        foreach ($list as $key=>$val){
            $val['user']->hidden(['password', 'salt']);
           $goods =  $deviceOrderDetailModel->where('order_id',$val['id'])->select();
           foreach ($goods as $k=>$v){
               $goods[$k]['cover'] = cdnurl($v['cover'],true);
           }
           $list[$key]['goods'] = $goods;
           $list[$key]['status'] = $deviceOrderModel->getStatus($val['status']);
        }
        $this->success('',$list);
    }

    public function detail()
    {

        $order_no = $this->request->param('order_no');
        $orderModel = new Deviceorder();
        $order = $orderModel->where('order_no',$order_no)->find();
        if(!$order) $this->error("订单不存在");

        $deviceOrderDetailModel = new Deviceorderdetail();

        $deviceModel = new \app\admin\model\Device();

        $userModel = new \app\admin\model\User();
        $user = $userModel->where('id',$order['user_id'])->find();
        $order['username'] = $user['username'];
        $order['mobile'] = $user['mobile'];
        $goods =  $deviceOrderDetailModel->where('order_id',$order['id'])->select();
        foreach ($goods as $k=>$v){
            $goods[$k]['cover'] = cdnurl($v['cover'],true);
        }
        $device = $deviceModel->where('device_id',$order['device_id'])->find();
        $order['device'] = $device;
        $order['status'] = $orderModel->getStatus($order['status']);
        $order['goods'] = $goods;
        $this->success('',$order);

    }

    public function refund()
    {

        $order_no = $this->request->param('order_no');
        $orderModel = new Deviceorder();
        $order = $orderModel->where('order_no',$order_no)->find();
        $isOk = -1;
        if($order['status'] == 'payed'){
            $isOk = 1;
        }
        if($order['status'] == 'fail'){
            $isOk = 1;
        }
        if($isOk == -1){
            $this->error("此订单不符合退款状态");
        }


        $req = array();
        $req['mno']=$order['mno'];
        $req['ordNo']= getOrderSn();
        $req['origOrderNo']= $order['order_no'];
        $req['amt']=$order['price'];


        error_log("==refund req===".json_encode($req));
        $vbillService = new VbillService();
        $ret = $vbillService->refund($req);
        error_log("=====refund=sxf==".$ret);
        $result = json_decode($ret,true);
        if($result['respData']['bizCode'] != "0000"){
            $this->error($result['respData']['bizMsg']);
        }
        $orderGoodModel = new Deviceorderdetail();
        $orderModel->where('id',$order['id'])->update(['status'=>'refund']);
        $orderGoodModel->where('order_id',$order['id'])->update(['status'=>'refund']);

        $this->success('处理成功');
    }

    public function shipAgain()
    {

        $order_no = $this->request->param('order_no');
        $user = $this->auth->getUser();
        $orderModel = new Deviceorder();
        $order = $orderModel->where('order_no',$order_no)->find();
        if($order['status'] != 'fail') $this->error("此订单不符合重新出货");
        if($order['agent_id'] != $user['id']) $this->error("无权限操作此订单");


        $subOrderNo = $order['device_id'].getSubOrderNo();
        $orderModel->where('id',$order['id'])->update(['sub_order_no'=>$subOrderNo]);

        $deviceOrderDetailModel = new Deviceorderdetail();
        $deviceOrderDetailModel->where('order_id',$order['id'])->update(['order_no'=>$subOrderNo]);

        $orderDetail = $deviceOrderDetailModel->where('order_id',$order['id'])->find();
        $req = array();
        $req['order_no'] = $order['sub_order_no'];
        $req['slot'] =$orderDetail['slot'];
        $req['device_id'] =  $order['device_id'];
        $req['user_id'] =  $user['id'];

        $deviceService = new DeviceService();
        $deviceService->ship($req);

        $this->success('成功');

    }

    public function updateOrderMark()
    {
        $order_no = $this->request->param('order_no');
        $mark = $this->request->param('mark');
        $orderModel = new Deviceorder();
        $orderDetailModel = new Deviceorderdetail();
        $order = $orderModel->where('order_no',$order_no)->find();
        $orderModel->where('id',$order['id'])->update(['status'=>'done','mark'=>$mark]);
        $orderDetailModel->where('order_id',$order['id'])->update(['status'=>'done']);

        $this->success('成功');
    }
}
